<template>
  <div
    class="square_header2 flex align_center"
  >
    <div
      class="square_header_item square_header_item2"
      :class="form.sort == index ? 'underline ' : ''"
      v-for="(item,index) in type"
      :key="index"
      @click="checkActiveType(index)"
    >
      <span
        :class="form.sort == index ? 'content4 color1 zIndex fontBold ' : 'default2'"
      >{{ item.name }}</span>
    </div>
  </div>
  <div>
    <van-pull-refresh
      v-model="refreshing"
      disabled
      @refresh="onRefresh"
      class="refresh-content"
    >
      <van-list
        class="content-list"
        v-model:loading="loading"
        :finished="finished"
        @load="loadMore"
        :immediate-check="false"
      >
        <div
          class="college_list"
          v-for="(item, index) in data"
          :key="index"
          @click="goCurriculum(item.id)"
        >
          <div class="college_list_box">
            <div>
              <img
                class="college_img"
                :src="item.tpic"
              >
            </div>
            <div class="college_info">
              <p
                v-if="item.type === 1"
                class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
              >
                <van-tag
                  style="height:18px;width:55px;justify-content: center;"
                  class="tagleft"
                  color="#EE762A"
                  type="primary"
                >
                  预告
                </van-tag>
                {{ item.title }}
              </p>
              <p
                v-if="item.type === 2"
                class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
              >
                <van-tag
                  style="height:18px;width:55px;justify-content: center;"
                  class="tagleft"
                  color="#F44A3D"
                  type="primary"
                >
                  <img
                    class="radio"
                    src="@/assets/img/radio.gif"
                  >直播中
                </van-tag>
                {{ item.title }}
              </p>
              <p
                v-if="item.type === 3"
                class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
              >
                <van-tag
                  style="height:18px;width:55px;justify-content: center;"
                  class="tagleft"
                  color="#2A90EF"
                  type="primary"
                >
                  回放
                </van-tag>
                {{ item.title }}
              </p>
              <p
                v-if="item.type === 4"
                class="college_list_box_p van-multi-ellipsis--l2 color1 fontBold content1"
              >
                <van-tag
                  style="height:18px;width:55px;justify-content: center;"
                  class="tagleft"
                  color="#2A90EF"
                  type="primary"
                >
                  <img
                    class="radio"
                    src="@/assets/img/radio.gif"
                  >录播
                </van-tag>
                {{ item.title }}
              </p>
              <div class="content2 font400 color6">
                <span>直播时间:{{ item.start_time }}</span>
                <!-- <span class=" left12">限时免费</span> -->
              </div>
            </div>
          </div>
        </div>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script setup lang="ts">
// 1.预告 2.直播中 3.回放 4.录播
import { ref, computed, watch } from 'vue'
import { courseList, getDemandType } from '@/api/index'
import { useRouter, useRoute } from 'vue-router'
const router = useRouter()
const route = useRoute()
const loading: any = ref(false)
const finished: any = ref(false)
const refreshing: any = ref(false)
const data: any = ref([])
const form: any = ref({
  page: 1,
  pageSize: 10,
  sort: route.query.course || 0
})
const type: any = ref([])

const selectedDemandTypeId = computed(() => {
  return route.query.course || 0
})

watch(selectedDemandTypeId, (newVal, oldVal) => {
  onRefresh()
})

const getType = async () => {
  type.value = await getDemandType({ type: 'course', is_all: 1 })
}

getType()

const checkActiveType = (index: any) => {
  form.value.sort = index
  router.replace({
    path: '/find',
    query: { f: route.query.f, course: index }
  })
}

const onRefresh = async () => {
  form.value.page = 1
  const list: any = await loadDemandList()
  data.value = list
  refreshing.value = false
}

const loadMore = async () => {
  form.value.page = form.value.page + 1
  const list: any = await loadDemandList()
  data.value = [...data.value, ...list]
  refreshing.value = false
}

const loadDemandList = async () => {
  // 异步更新数据
  const res: any =
    (await courseList({ ...form.value })) || []
  if (!Array.isArray(res) && typeof res.list === 'object') {
    const keys: any = Object.keys(res.list)
    const newRes: any = keys.map((k: any) => res.list[k])
    finished.value = newRes.length < form.value.pageSize
    return newRes
  }
  finished.value = res.list.length < form.value.pageSize
  return res.list
}

onRefresh()

const goCurriculum = async (id: any) => {
  router.push({ path: '/find/curriculum', query: { id: id } })
}
</script>

<style lang="less" scoped>
.home {
  margin: 0 15px;
  .request-btn {
    font-size: 16px;
    border: none;
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2;
    width: 149px;
    height: 44px;
    background: #ff7a1f;
    box-shadow: 0px 2px 10px 0px rgba(238, 111, 42, 0.5);
    border-radius: 5px;
  }

  .onSearch {
    border-radius: 5px;
    margin-bottom: 20px;
    .label_icon {
      overflow: hidden;
      display: flex;
      align-items: center;
      .icon {
        width: 34px;
        height: 34px;
        object-fit: cover;
        margin-right: 14px;
      }
      span {
        margin: 0 14px 0 0;
        color: #d8d8d8;
      }
    }

    .van-field__left-icon {
      display: none;
    }
  }
}

.square_header {
  margin-top: 26px;
  .square_header_item {
    margin-right: 29px;
    .default {
      font-size: 19px;
      font-weight: bold;
      color: #454757;
    }
  }
}

.square_header2 {
  // margin-top: 22px;
  .square_header_item2 {
    margin-right: 32px;
    .default2 {
      font-size: 14px;
      font-weight: bold;
      color: #454757;
    }
  }
}

.college_list {
  width: 100%;
  margin: 15px auto;
  background-color: #fff;
  border-radius: 5px;
  .college_list_box {
    display: flex;
    padding: 15px;
    .college_info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .radio {
        width: 10px;
        height: 10px;
        margin-right: 5px;
      }
    }
    .college_list_box_p {
      line-height: 20px;
    }
    // justify-content: space-between;
    .college_img {
      width: 100px;
      height: 70px;
      border-radius: 5px;
      object-fit: cover;
      margin-right: 14px;
    }
    .left12 {
      margin-left: 10px;
      color:#EE762A;
    }
  }
}

.user-card-item {
  width: 90%;
  height: 87px;
  background: #ffffff;
  border-radius: 10px;
  margin: 10px auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  .excellent {
    width: 25px;
    height: 25px;
    margin-right: 14px;
  }

  .left {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-left: 14px;
    .info {
      margin-left: 10px;
    }
  }
  .avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
  }
  .qiye {
    width: 14px;
    height: 14px;
    margin: 0 4px 0 2px;
    display: inline-block;
    vertical-align: middle;
  }
  .position {
    font-size: 12px;
    font-weight: 400;
    color: #9a9a9a;
  }
  .name {
    font-size: 14px;
    font-weight: bold;
    color: #343434;
  }
  .w {
    width: 200px;
    display: inline-block;
    vertical-align: middle;
  }
  .company {
    font-size: 12px;
    font-weight: 400;
    color: #9a9a9a;
    margin: 5px 0;
  }
  .tags {
    margin-right: 9px;
  }
  p {
    margin: 0;
  }
}
@media (min-width: 750px) {
  .home {
  margin: 0 15px;
  .request-btn {
    font-size: 16px;
    border: none;
    position: fixed;
    bottom: 100px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 2;
    width: 149px;
    height: 44px;
    background: #ff7a1f;
    box-shadow: 0px 2px 10px 0px rgba(238, 111, 42, 0.5);
    border-radius: 5px;
  }

  .onSearch {
    border-radius: 5px;
    margin-bottom: 20px;
    .label_icon {
      overflow: hidden;
      display: flex;
      align-items: center;
      .icon {
        width: 34px;
        height: 34px;
        object-fit: cover;
        margin-right: 14px;
      }
      span {
        margin: 0 14px 0 0;
        color: #d8d8d8;
      }
    }

    .van-field__left-icon {
      display: none;
    }
  }
}

.square_header {
  margin-top: 26px;
  .square_header_item {
    margin-right: 29px;
    .default {
      font-size: 19px;
      font-weight: bold;
      color: #454757;
    }
  }
}

.square_header2 {
  // margin-top: 22px;
  .square_header_item2 {
    margin-right: 32px;
    .default2 {
      font-size: 14px;
      font-weight: bold;
      color: #454757;
    }
  }
}

.college_list {
  width: 100%;
  margin: 15px auto;
  background-color: #fff;
  border-radius: 5px;
  .college_list_box {
    display: flex;
    padding: 15px;
    .college_info {
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      .radio {
        width: 10px;
        height: 10px;
        margin-right: 5px;
      }
    }
    .college_list_box_p {
      line-height: 20px;
    }
    // justify-content: space-between;
    .college_img {
      width: 100px;
      height: 70px;
      border-radius: 5px;
      object-fit: cover;
      margin-right: 14px;
    }
    .left12 {
      margin-left: 10px;
      color:#EE762A;
    }
  }
}

.user-card-item {
  width: 90%;
  height: 87px;
  background: #ffffff;
  border-radius: 10px;
  margin: 10px auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: relative;
  .excellent {
    width: 25px;
    height: 25px;
    margin-right: 14px;
  }

  .left {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-left: 14px;
    .info {
      margin-left: 10px;
    }
  }
  .avatar {
    width: 42px;
    height: 42px;
    border-radius: 50%;
  }
  .qiye {
    width: 14px;
    height: 14px;
    margin: 0 4px 0 2px;
    display: inline-block;
    vertical-align: middle;
  }
  .position {
    font-size: 12px;
    font-weight: 400;
    color: #9a9a9a;
  }
  .name {
    font-size: 14px;
    font-weight: bold;
    color: #343434;
  }
  .w {
    width: 200px;
    display: inline-block;
    vertical-align: middle;
  }
  .company {
    font-size: 12px;
    font-weight: 400;
    color: #9a9a9a;
    margin: 5px 0;
  }
  .tags {
    margin-right: 9px;
  }
  p {
    margin: 0;
  }
}
}
</style>
